package com.data;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.bean.MsgBean;
import com.constant.Constant;
import com.net.AsyncRequest;
import com.net.ClientException;
import com.net.RequestParameter;

public final class MsgData extends BaseData {

	private ArrayList<MsgBean> mMsgList;
	
	@Override
	public void startParse(RequestParameter parameter) throws Exception,
			ClientException {
		String rs = AsyncRequest.openUrl(Constant.SERVER_URL + "/message", AsyncRequest.HTTP_GET, parameter);
		if(rs == null)
			return;
		
		
		
		JSONArray array = new JSONArray(rs);
		if(array.length() == 0)
			return;
		
		mMsgList = new ArrayList<MsgBean>();
		for(int i = 0, len = array.length(); i < len; i++ ) {
			JSONObject obj = array.optJSONObject(i);//对消息的处理
			MsgBean bean = new MsgBean();
			bean.id	=	obj.getLong("id");
			bean.title	=	obj.getString("title");
			bean.desc	=	obj.getString("desc");
			bean.linkurl	=	obj.getString("linkurl");
			bean.type_msg	=	obj.getInt("type_msg");
			if(!obj.isNull("imgs")) {
				JSONArray imgs = obj.getJSONArray("imgs");
				bean.imgList = new ArrayList<String>();
				for(int j = 0, jLen = imgs.length(); j < jLen; j++ ) {
					
					bean.imgList.add(imgs.optString(j));
				} // 
			} // if
			mMsgList.add(bean);
		} //
		
	}

	public ArrayList<MsgBean> getMsgList() {
		return this.mMsgList;
	}
	
	public void printData() {
		for(int i = 0; i < mMsgList.size(); i++) {
			MsgBean bean = mMsgList.get(i);
			
			System.out.println("title:" + bean.title);//这个是输出到什么地方？
			System.out.println("desc: " + bean.desc);
			System.out.println("linkurl:" + bean.linkurl);
			System.out.println("type_msg:" + bean.type_msg);
			System.out.println("id:" + bean.id);
			if(bean.imgList != null) {
				for(int j = 0; j < bean.imgList.size(); j++) {
					System.out.println(bean.imgList.get(j));
				}
			}
		}
	}
}
